package com.cyurs.netty.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Author: Cyrus
 * @CreateDate: 2020/11/17
 */
public class NIOClient {
    public static void main(String[] args) throws Exception{
        //开启socketChannel
        SocketChannel socketChannel = SocketChannel.open();
        //设置为非阻塞
        socketChannel.configureBlocking(false);
        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 6666);

        if (!socketChannel.connect(socketAddress)) {
            while (!socketChannel.finishConnect()) {
                System.out.println("连接需要时间，此时不会阻塞，可以做其他工作");
            }
        }
        String str = "hello,Cyrus";
        //Wraps a byte array into a buffer 包裹一个字节数组
        ByteBuffer buffer = ByteBuffer.wrap(str.getBytes());
        //将buffer数据写到通道中
        socketChannel.write(buffer);
        System.in.read(); //运行停在这里
    }
}
